package me.pplay.playtalk.activity.store;

import me.pplay.playtalk.R;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.view.CommonTabTopLayout;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.LinearLayout;

public class StoreActivity extends ActivityGroup {
	
	public static StoreActivity group;
	private LocalActivityManager mLocalActivityManager;
	private CommonTabTopLayout tab;
	private LinearLayout container;
	private int position = 0;
	
	Handler handler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case CommonTabTopLayout.SETADAPTER:
					position = msg.arg1;
					if(position == 0){
						launchActivity("Store_topActivity", new Intent(StoreActivity.this, Store_topActivity.class), false);
					}else if(position == 1){
						launchActivity("Store_categoryActivity", new Intent(StoreActivity.this, Store_categoryActivity.class), false);
					}else if(position == 2){
						launchActivity("Store_purchasesActivity", new Intent(StoreActivity.this, Store_purchasesActivity.class), false);
					}
					break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_store);
		findViews();
		initDatas();
	}
	
	private void findViews(){
		tab 					= (CommonTabTopLayout)findViewById(R.id.tab);
		container				= (LinearLayout)findViewById(R.id.container);
	}
	
	private void initDatas(){
		group					= this;
		mLocalActivityManager	= getLocalActivityManager();
		tab.setTabTopLayout(Globals.width/3, getResources().getStringArray(R.array.array_store_tab), null, handler);
		launchActivity("Store_topActivity", new Intent(this, Store_topActivity.class),false);
	}
	
	
	public void launchActivity(String ViewID, Intent intent, boolean isFlag) {
		container.removeAllViews();
		if(isFlag)
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		container.addView(mLocalActivityManager.startActivity(ViewID, intent).getDecorView());
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		this.
//		if(mLocalActivityManager.getCurrentActivity() != this){
			this.mLocalActivityManager.getCurrentActivity().onBackPressed();
			return false;
//		}
//		return false;
	}

}
